//存储数据
const setStorage = (key, value) => {
  try {
    wx.setStorageSync(key, value)
  } catch (error) {
    console.error(`存储 ${key}数据发生了异常`, error)
  }
}

//读取数据
const getStorage = (key) => {
  try {
    const value = wx.getStorageSync(key)
    if (value) return value
  } catch (error) {
    console.error(`读取 ${key}数据发生了异常`, error)
  }
}

//移除本地数据
const removeStorage = (key) => {
  try {
    wx.removeStorageSync(key)
  } catch (error) {
    console.error(`移除 ${key}数据发生了异常`, error)
  }
}

//从本地清空所有数据
const clearStorage = (key) => {
  try {
    wx.clearStorageSync(key)
  } catch (error) {
    console.error(`移除 ${key}数据发生了异常`, error)
  }
}

//异步操作API
const asyncSetStorage = (key, data) => {
  return new Promise((resolve) => {
    wx.setStorage({
      key,
      data,
      complete(res) {
        resolve(res)
      }
    })
  })
}
//异步读取本地API
const asyncGetStorage = (key) => {
  return new Promise((resolve) => {
    wx.getStorage({
      key,
      complete(res) {
        resolve(res)
      }
    })
  })
}

//异步移除本地api
const asyncRemoveStorage = (key) => {
  return new Promise((resolve) => {
    wx.removeStorage({
      key,
      complete(res) {
        resolve(res)
      }
    })
  })
}

//异步清空本地数据
const asyncClearStorage = () => {
  return new Promise((resolve) => {
    wx.clearStorage({
      complete(res) {
        resolve(res)
      }
    })
  })
}

export { setStorage, getStorage, removeStorage, clearStorage, asyncSetStorage, asyncGetStorage, asyncRemoveStorage, asyncClearStorage }
